<!DOCTYPE HTML>
<html lang="en-US" >
    
    <head>
        
        <meta charset="UTF-8">
        <title>内存管理简介 | 内存管理简介</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="Book generated using GitBook">
        <meta name="generator" content="GitBook 1.0.3">
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">
        
    
    
    
    <link rel="next" href="./1.html" />
    
    

        
    </head>
    <body>
        
        
<link rel="stylesheet" href="gitbook/style.css">


        
    <div class="book"  data-level="0" data-basepath="." data-revision="1442488952143">
    

<div class="book-summary">
    <div class="book-search">
        <input type="text" placeholder="Type to search" class="form-control" />
    </div>
    <ul class="summary">
        
    	
    	
    	

        

        
    
        
        <li class="chapter active" data-level="0" data-path="index.html">
            
                
                    <a href="./index.html">
                        <i class="fa fa-check"></i>
                        
                         内存管理简介
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="1" data-path="1.html">
            
                
                    <a href="./1.html">
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                         引用计数器
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="2" data-path="dealloc/README.html">
            
                
                    <a href="./dealloc/README.html">
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                         dealloc方法
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="3" data-path="3.html">
            
                
                    <a href="./3.html">
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                         野指针\空指针
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="4" data-path="xcode/README.html">
            
                
                    <a href="./xcode/README.html">
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                         Xcode设置
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="5" data-path="5.html">
            
                
                    <a href="./5.html">
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                         内存管理原则
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="6" data-path="property/README.html">
            
                
                    <a href="./property/README.html">
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                         @property参数
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="7" data-path="10.html">
            
                
                    <a href="./10.html">
                        <i class="fa fa-check"></i>
                        
                            <b>7.</b>
                        
                         @Property练习
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="8" data-path="class/README.html">
            
                
                    <a href="./class/README.html">
                        <i class="fa fa-check"></i>
                        
                            <b>8.</b>
                        
                         @class
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="9" data-path="retian/README.html">
            
                
                    <a href="./retian/README.html">
                        <i class="fa fa-check"></i>
                        
                            <b>9.</b>
                        
                         循环retian
                    </a>
                
            
            
        </li>
    


        
        <li class="divider"></li>
        <li>
            <a href="http://www.gitbook.io/" target="blank" class="gitbook-link">Published using GitBook</a>
        </li>
        
    </ul>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header">
    <!-- Actions Left -->
    <a href="#" class="btn pull-left toggle-summary" aria-label="Toggle summary"><i class="fa fa-align-justify"></i></a>
    <a href="#" class="btn pull-left toggle-search" aria-label="Toggle search"><i class="fa fa-search"></i></a>
    
    <div id="font-settings-wrapper" class="dropdown pull-left">
        <a href="#" class="btn toggle-dropdown" aria-label="Toggle font settings"><i class="fa fa-font"></i>
        </a>
        <div class="dropdown-menu font-settings">
    <div class="dropdown-caret">
        <span class="caret-outer"></span>
        <span class="caret-inner"></span>
    </div>

    <div class="buttons">
        <button type="button" id="reduce-font-size" class="button size-2">A</button>
        <button type="button" id="enlarge-font-size" class="button size-2">A</button>
    </div>

    <div class="buttons font-family-list">
        <button type="button" data-font="0" class="button">Serif</button>
        <button type="button" data-font="1" class="button">Sans</button>
    </div>

    <div class="buttons color-theme-list">
        <button type="button" id="color-theme-preview-0" class="button size-3" data-theme="0">White</button>
        <button type="button" id="color-theme-preview-1" class="button size-3" data-theme="1">Sepia</button>
        <button type="button" id="color-theme-preview-2" class="button size-3" data-theme="2">Night</button>
    </div>
</div>

    </div>

    <!-- Actions Right -->
    
    <div class="dropdown pull-right">
        <a href="#" class="btn toggle-dropdown" aria-label="Toggle share dropdown"><i class="fa fa-share-alt"></i>
        </a>
        <div class="dropdown-menu font-settings dropdown-left">
            <div class="dropdown-caret">
                <span class="caret-outer"></span>
                <span class="caret-inner"></span>
            </div>
            <div class="buttons">
                <button type="button" data-sharing="twitter" class="button">Twitter</button>
                <button type="button" data-sharing="google-plus" class="button">Google</button>
                <button type="button" data-sharing="facebook" class="button">Facebook</button>
                <button type="button" data-sharing="weibo" class="button">Weibo</button>
                <button type="button" data-sharing="instapaper" class="button">Instapaper</button>
            </div>
        </div>
    </div>
    

    
    <a href="#" target="_blank" class="btn pull-right google-plus-sharing-link sharing-link" data-sharing="google-plus" aria-label="Share on Google Plus"><i class="fa fa-google-plus"></i></a>
    
    
    <a href="#" target="_blank" class="btn pull-right facebook-sharing-link sharing-link" data-sharing="facebook" aria-label="Share on Facebook"><i class="fa fa-facebook"></i></a>
    
    
    <a href="#" target="_blank" class="btn pull-right twitter-sharing-link sharing-link" data-sharing="twitter" aria-label="Share on Twitter"><i class="fa fa-twitter"></i></a>
    
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="./" >内存管理简介</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-gitbook_252">
                    
                        <h1 id="">内存管理简介</h1>
<h2 id="">本小节知识点:</h2>
<ol>
<li>【了解】内存管理的重要性</li>
<li>【理解】什么是内存管理</li>
<li>【掌握】堆和栈</li>
</ol>
<hr>
<h2 id="1">1.内存管理的重要性</h2>
<ul>
<li><p>移动设备的内存极其有限，每个app所能占用的内存是有限制的</p>
</li>
<li><p>下列行为都会增加一个app的内存占用</p>
<ul>
<li>创建一个OC对象</li>
<li>定义一个变量</li>
<li>调用一个函数或者方法</li>
</ul>
</li>
<li><p>当app所占用的内存较多时，系统会发出内存警告，这时得回收一些不需要再使用的内存空间。比如回收一些不需要使用的对象、变量等</p>
</li>
<li><p>如果app占用内存过大, 系统可能会强制关闭app, 造成闪退现象, 影响用户体验</p>
</li>
</ul>
<hr>
<h2 id="2">2.什么是内存管理</h2>
<ul>
<li><p>如何回收那些不需要再使用的对象?</p>
<ul>
<li>那就得学会OC的内存管理</li>
</ul>
</li>
<li><p>所谓内存管理, 就是对内存进行管理, 涉及的操作有:</p>
<ul>
<li>分配内存 : 比如创建一个对象, 会增加内存占用</li>
<li>清除内存 : 比如销毁一个对象, 能减小内存占用</li>
</ul>
</li>
<li><p>内存管理的管理范围</p>
<ul>
<li>任何继承了NSObject的对象</li>
<li>对其他非对象类型无效(int、char、float、double、struct、enum等 )</li>
</ul>
</li>
<li><p>只有OC对象才需要进行内存管理的本质原因</p>
<ul>
<li>OC对象存放于堆里面</li>
<li>非OC对象一般放在栈里面(栈内存会被系统自动回收)</li>
</ul>
</li>
</ul>
<hr>
<h2 id="3">3.堆和栈</h2>
<ul>
<li><p>栈（操作系统）：由操作系统自动分配释放，存放函数的参数值，局部变量的值等。其操作方式类似于数据结构中的栈(先进后出)；</p>
</li>
<li><p>堆（操作系统）：一般由程序员分配释放，若程序员不释放，程序结束时可能由OS回收，分配方式类似于链表。</p>
</li>
<li><p>示例:</p>
</li>
</ul>
<pre><code class="lang-objc">
<span class="hljs-keyword">int</span> main(<span class="hljs-keyword">int</span> argc, <span class="hljs-keyword">const</span> <span class="hljs-keyword">char</span> * argv[])
{
    <span class="hljs-keyword">@autoreleasepool</span> {
        <span class="hljs-keyword">int</span> a = <span class="hljs-number">10</span>; <span class="hljs-comment">// 栈</span>
        <span class="hljs-keyword">int</span> b = <span class="hljs-number">20</span>; <span class="hljs-comment">// 栈</span>
        <span class="hljs-comment">// p : 栈</span>
        <span class="hljs-comment">// Person对象(计数器==1) : 堆</span>
        Person *p = [[Person alloc] init];
    }
    <span class="hljs-comment">// 经过上一行代码后, 栈里面的变量a\b\c都会被回收</span>
    <span class="hljs-comment">// 但是堆里面的Person对象还会留在内存中,因为它是计数器依然是1</span>
    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}
</code></pre>
<p><img src="images/a0/dhz.png" alt=""></p>
<p><a href="http://baike.baidu.com/link?url=VJz-vKfY_ASVHCB17nWR_uQ4Adhuasl2WOrW4O7ZgFdgq5Tl_kBGfUYzbNSzkSBkHAbAH8qzfDpl5Wm4rxQ1Ka" target="_blank">更多关于堆栈信息</a></p>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        
        <a href="./1.html" class="navigation navigation-next navigation-unique" aria-label="Next page: 引用计数器"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="gitbook/app.js"></script>

    
    <script src="https://cdn.mathjax.org/mathjax/2.4-latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
    

    
    <script src="gitbook/plugins/gitbook-plugin-mathjax/plugin.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"fontSettings":{"theme":null,"family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        
    </body>
    
</html>
